#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/8/8 9:21
@File: check.py
"""
from qturboframe.lib.webdriver import SeleniumError
from qturboframe.public_ui.base_element import BaseElement


from BladeFerm.Lib.Common import logger


class CheckSingle(BaseElement):
    check_class_flag = 'h-checkbox-wrapper'
    check_class_status = 'h-checkbox-wrapper-checked'

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)
        class_list = self._element.get_attribute('class').split(' ')
        logger.debug(f'class_list: {class_list}')
        if self.check_class_flag not in class_list:
            self._element = self._element.find_element_by_attribute('xpath', './span[contains(@class,"h-checkbox")]')
        self.check_input = self._element.find_element_by_attribute('xpath', './/input[@type="checkbox"]')

    def get_cur_status(self):
        '''
        判断check控件是否是选中状态
        :return:
        '''
        class_content = self._element.get_attribute('class')
        class_list = class_content.split(' ')
        logger.debug(f'控件的属性：{class_list}')
        if self.check_class_flag not in class_list:
            raise SeleniumError(f'{self.locator_by}:{self.locator}定位错误，属性没有{self.check_class_flag}')
        if self.check_class_status in class_list:
            return True
        else:
            return False

    def set_status(self, flag=True):
        '''
        设置状态
        :param flag:
        :return:
        '''
        cur_flag = self.get_cur_status()
        if cur_flag != flag:
            self.check_input.click()
        logger.info(f'设置状态为：{flag}')

    def is_default(self, flag=True):
        '''
        检查是否是默认的状态
        :param flag:
        :return:
        '''
        cur_flag = self.get_cur_status()
        if flag == cur_flag:
            return True
        else:
            raise SeleniumError(f'Check控件的默认状态与期望{flag}不符！')